// Verbs

function conjugate() {
	var stem = document.verbf.stem.value;
	if ( stem == "" ) {
		alert("You must enter a dictionary form stem.");
		return;
	}
	var tense = "present";
	for ( i = 0; i < document.verbf.tense.length; i++ ) {
		if ( document.verbf.tense[i].checked ) {
			tense = document.verbf.tense[i].value;
		}
	}
	var politeness = "standard"
	for ( i = 0; i < document.verbf.politeness.length; i++ ) {
		if ( document.verbf.politeness[i].checked ) {
			politeness = document.verbf.politeness[i].value;
		}
	}
	var params = "tense=" + tense + "&politeness=" + politeness;
	post(showConjugation,"genverb.php",params);
}

function showConjugation() {
	if (xmlHttp.readyState == 4) {
		var suffix = xmlHttp.responseText;
		var stem = document.verbf.stem.value;
		var html = "<p>Stem: <span class=\"bold\">" + stem + "</span><br>";
		var html = "Suffix: <span class=\"bold\">" + suffix + "</span><br>";
		html += "Conjugation: <span class=\"bold\">" + appendSuffix(stem, suffix) + "</span>";
		html +="</p>";
		document.getElementById("cresults").innerHTML = html;
	}
}

function appendSuffix(stem,suffix) {
	
	// TODO: Handle exception cases (honorific, spelling changes etc.)
	var lvt = decomposeHangul(stem.charCodeAt(stem.length-1));
	if (endsInHa(lvt)) {
		return "Ends in 하";
	} else if (endsInVowel(lvt)) {
		return "Ends in vowel";
	} else {
		return "Ends in consonant";
	}	
}

// See: http://www.unicode.org/unicode/reports/tr15/

function decomposeHangul(s) {

	var SBase = 0xAC00, LBase = 0x1100, VBase = 0x1161, TBase = 0x11A7,
	LCount = 19, VCount = 21, TCount = 28,
	NCount = VCount * TCount,   // 588
	SCount = LCount * NCount;   // 11172

    var SIndex = s - SBase;
    if (SIndex < 0 || SIndex >= SCount) {
        return s;
    }
    var L = parseInt( LBase + SIndex / NCount );
    var V = parseInt( VBase + (SIndex % NCount) / TCount );
    var T = parseInt( TBase + SIndex % TCount );
	
    return [L,V,T];
}

function endsInHa(lvt) {
	return( lvt[0] == 4370 && lvt[1] == 4449 && lvt[2] == 4519 );
}

function endsInVowel(lvt) {
	return ( lvt[2] == 4519 );
}

/*public static String composeHangul(String source) {
        int len = source.length();
        if (len == 0) return "";
        StringBuffer result = new StringBuffer();
        char last = source.charAt(0);            // copy first char
        result.append(last);

        for (int i = 1; i < len; ++i) {
            char ch = source.charAt(i);

            // 1. check to see if two current characters are L and V

            int LIndex = last - LBase;
            if (0 <= LIndex && LIndex < LCount) {
                int VIndex = ch - VBase;
                if (0 <= VIndex && VIndex < VCount) {

                    // make syllable of form LV

                    last = (char)(SBase + (LIndex * VCount + VIndex) * TCount);

                    result.setCharAt(result.length()-1, last); // reset last
                    continue; // discard ch
                }
            }


            // 2. check to see if two current characters are LV and T

            int SIndex = last - SBase;
            if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0) {
                int TIndex = ch - TBase;
                if (0 < TIndex && TIndex < TCount) {

                    // make syllable of form LVT

                    last += TIndex;
                    result.setCharAt(result.length()-1, last); // reset last
                    continue; // discard ch
                }
            }
            // if neither case was true, just add the character
            last = ch;
            result.append(ch);
        }
        return result.toString();
    }*/